package cz.fit.vutbr.pis2012.augeaswh.control.ekonom.supplier;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import cz.fit.vutbr.pis2012.augeaswh.managers.SupplierManager;
import cz.fit.vutbr.pis2012.augeaswh.persistence.BankAccount;
import cz.fit.vutbr.pis2012.augeaswh.persistence.Supplier;

@ManagedBean
@SessionScoped
public class SupplierEditorBean implements Serializable {
	@EJB
	private SupplierManager sm;
	
	private static final long serialVersionUID = -6000365533104340973L;
	private Supplier supplier;
	private String name;
	private String ico;
	private String dic;
	private String accountNumber;
	private String bankName;
	private String iban;
	

	public SupplierEditorBean() {
		setSupplier(null);
	}
	
	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
		name=ico=dic=accountNumber=bankName=iban="";
		if(this.supplier!=null) {
			this.name=supplier.getName();
			this.ico=supplier.getIco();
			this.dic=supplier.getDic();
			if(supplier.getBankAccount()!=null) {
				this.accountNumber=supplier.getBankAccount().getAccountNumber();
				this.bankName=supplier.getBankAccount().getBankName();
				this.iban=supplier.getBankAccount().getIban();
			}
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIco() {
		return ico;
	}

	public void setIco(String ico) {
		this.ico = ico;
	}

	public String getDic() {
		return dic;
	}

	public void setDic(String dic) {
		this.dic = dic;
	}

	public String getAccountNumber() {
		return accountNumber;
	}

	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getIban() {
		return iban;
	}

	public void setIban(String iban) {
		this.iban = iban;
	}
	
	public String submit() {
		
		if(supplier==null) {
			this.supplier=new Supplier();
		}
		this.supplier.setName(name);
		this.supplier.setIco(ico);
		this.supplier.setDic(dic);
		if(this.supplier.getBankAccount()==null)
			this.supplier.setBankAccount(new BankAccount());
		BankAccount ba=this.supplier.getBankAccount();
		ba.setAccountNumber(accountNumber);
		ba.setBankName(bankName);
		ba.setIban(iban);
		sm.save(supplier);
		setSupplier(null);
		return "back";
	}
	
	public String getTitle() {
		if(this.supplier==null) {
			return "New supplier";
		}
		else {
			return "Edited supplier \""+this.supplier.getName()+"\"";
		}			
	}
}
